﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using PstBakup2013Server;
using System;
using System.Xml;

namespace Unit_Tests_PstBakup2013Server
{
    [TestClass]
    public class PstFileTest
    {
        [TestMethod]
        public void PstFileConstructorTest()
        {
            string strNode =
          "<File>" +
            "<LocalPath>{0}</LocalPath>" +
            "<RemotePath>{1}</RemotePath>" +
            "<Backup>{2}</Backup>" +
            "<ScheduledForBackup>{3}</ScheduledForBackup>" +
            "<Result>{4}</Result>" +
            "<Size>{5}</Size>" +
            "<CompressedSize>{6}</CompressedSize>" +
            "<Compressed>{7}</Compressed>" +
            "<StartTime>{8}</StartTime>" +
            "<EndTime>{9}</EndTime>" +
            "<ErrorMessage>{10}</ErrorMessage>" +
          "</File>";

            object[] valuesSets1 = new object[]
            {
                @"C:\Users\Courtel\AppData\Local\Microsoft\Outlook\Archive1.pst",
                @"\\192.168.0.250\Pst Files\Courtel\Archive1.pst",
                true,
                true,
                0,
                456124789542,
                231345714563,
                true,
                "15/03/2015 20:37:09",
                "15/03/2015 21:12:12",
                ""
            };

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(String.Format(strNode, valuesSets1));
            XmlNode fileNode = doc.DocumentElement;

            PstFile pstFile = new PstFile(fileNode);
            Assert.AreEqual(@"C:\Users\Courtel\AppData\Local\Microsoft\Outlook\Archive1.pst", pstFile.LocalPath);
            Assert.AreEqual(@"\\192.168.0.250\Pst Files\Courtel\Archive1.pst", pstFile.RemotePath);
            Assert.IsTrue(pstFile.IsSetToBackup);
            Assert.IsTrue(pstFile.IsScheduledForBackup);
            Assert.AreEqual(0, pstFile.ResultID);
            Assert.AreEqual(456124789542, pstFile.Size);
            Assert.AreEqual(231345714563, pstFile.CompressedSize);
            Assert.IsTrue(pstFile.IsCompressed);
            Assert.AreEqual("15/03/2015 20:37:09", pstFile.StartTime.ToString());
            Assert.AreEqual("15/03/2015 21:12:12", pstFile.EndTime.ToString());
            Assert.AreEqual(String.Empty, pstFile.ErrorMessage);

            pstFile = null;
            doc.LoadXml("<Junk>hzeheuityeui</Junk>");
            fileNode = doc.DocumentElement;

            pstFile = new PstFile(fileNode);
            Assert.AreNotEqual(pstFile, null);
            Assert.AreEqual(String.Empty, pstFile.LocalPath);
            Assert.AreEqual(String.Empty, pstFile.RemotePath);
        }

        [TestMethod]
        public void GetPropertiesValuesTest()
        {
            string strNode =
             "<File>" +
               "<LocalPath>{0}</LocalPath>" +
               "<RemotePath>{1}</RemotePath>" +
               "<Backup>{2}</Backup>" +
               "<ScheduledForBackup>{3}</ScheduledForBackup>" +
               "<Result>{4}</Result>" +
               "<Size>{5}</Size>" +
               "<CompressedSize>{6}</CompressedSize>" +
               "<Compressed>{7}</Compressed>" +
               "<StartTime>{8}</StartTime>" +
               "<EndTime>{9}</EndTime>" +
               "<ErrorMessage>{10}</ErrorMessage>" +
             "</File>";

            object[] valuesSets1 = new object[]
            {
                @"C:\Users\Courtel\AppData\Local\Microsoft\Outlook\Archive1.pst",
                @"\\192.168.0.250\Pst Files\Courtel\Archive1.pst",
                true,
                true,
                0,
                456124789542,
                231345714563,
                true,
                "15/03/2015 20:37:09",
                "15/03/2015 21:12:12",
                ""
            };

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(String.Format(strNode, valuesSets1));
            XmlNode fileNode = doc.DocumentElement;

            PstFile pstFile = new PstFile(fileNode);
            String propertiesValues = pstFile.GetPropertiesValues();

            //  0   this.LocalPath
            //  1   this.RemotePath
            //  2   this.IsSetToBackup
            //  3   this.IsScheduledForBackup
            //  4   this.IsSavedAtLeastOnce
            //  5   this.LastDateSaved
            //  6   (int)((float)this.Size / 1024f/1024f)
            //  7   (int)((float)this.CompressedSize / 1024f/1024f)
            //  8   this.IsCompressed
            //  9   this.CompressionRate
            //  10  this.StartTime
            //  11  this.EndTime
            //  12  this.BackupDuration
            //  13  this.BackupSpeed
            //  14  this.ResultID
            //  15  this.ErrorMessage

            string[] values = propertiesValues.Split(new char[] { ';' });
            Assert.AreEqual(@"C:\Users\Courtel\AppData\Local\Microsoft\Outlook\Archive1.pst", values[0]);
            Assert.AreEqual(@"\\192.168.0.250\Pst Files\Courtel\Archive1.pst", values[1]);
            Assert.IsTrue(Convert.ToBoolean(values[2]));
            Assert.IsTrue(Convert.ToBoolean(values[3]));
            Assert.AreEqual(String.Format("{0:0,0}", 456124789542 / 1024 / 1024), values[6]);
            Assert.AreEqual(String.Format("{0:0,0}", 231345714563 / 1024 / 1024), values[7]);
            Assert.IsTrue(Convert.ToBoolean(values[8]));
            Assert.AreEqual(String.Format("{0:##.##}%", (1f - 231345714563f / 456124789542f) * 100), values[9]);
            Assert.AreEqual("15/03/2015 20:37:09", values[10]);
            Assert.AreEqual("15/03/2015 21:12:12", values[11]);
            Assert.AreEqual(String.Format("{0:#.##}", 231345714563.0 / 1024.0 / 1024.0 / 2103.0), values[13]);

            pstFile = null;
            doc.LoadXml("<Junk>hzeheuityeui</Junk>");
            fileNode = doc.DocumentElement;

            pstFile = new PstFile(fileNode);
            values = pstFile.GetPropertiesValues().Split(new char[] { ';' });
            Assert.AreEqual("0%", values[9]);
        }
    }
}
